﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

namespace ERP.Hoadon
{
    public partial class viewERP_Giaohang : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
        public cs_erp_khachhang_sodoInfo hh = new cs_erp_khachhang_sodoInfo();
        public cs_erp_nhapkhoInfo nk = new cs_erp_nhapkhoInfo();
        public cs_erp_hanghoaInfo hanghoa = new cs_erp_hanghoaInfo();

        int count = 0;
        public int flag = 1;
        public viewERP_Giaohang()
        {
            InitializeComponent();
        }

        private void viewERP_Giaohang_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.Load_Info();
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void Load_Info()
        {
            this.kh.id_kh = this.hoadon.id_kh;
            this.kh.Get_Info();
            this.txtHoten.Text = this.kh.tenkhachhang;
            this.txtDiachi.Text = this.kh.diachi;
            this.txtDienthoai.Text = this.kh.dienthoai;
            this.txtHoadon.Text = this.hoadon.sohd;
            this.txtNgayban.EditValue = this.hoadon.ngayban;
            this.txtNgaythu.EditValue = this.hoadon.ngayhentra;
            //this.txtNgayhoanthanh.EditValue = this.hoadon.ngayhentra.AddDays(-2);
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            //this.hanghoa.Get_List = this.kh.id_kh;
            ds = this.hanghoa.Get_List_Giaohang(this.hoadon.id_hd);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }
        private void Save_Info()
        {
            this.nk.id_hd = this.hoadon.id_hd;
            //this.nk.makho = Convert.ToInt32(this.cboKhonhap.EditValue.ToString().Trim());

            this.nk.Get_Info();
            this.nk.manx = 2;
            this.nk.id_lannhap = -1;
            this.nk.ngaynhap = DateTime.Now;
            this.nk.nguoinhap = this.user.id_nv;
            this.nk.noidung = "Xuất kho Hóa đơn: " + this.hoadon.sohd;
            this.nk.sohd = this.hoadon.sohd;
            this.nk.ghichu = "Xuất thành phẩm bàn giao Khách hàng và trừ vào kho thành phẩm";
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.hoadon.trangthai = 5;
            this.hoadon.ExcuteProcedure(2);
            this.Save_Info();
            this.nk.ExcuteProcedure(1);
            
            XtraMessageBox.Show("Xuất kho thành phẩm bàn giao cho Khách hàng thành công!", "Thông báo");
            this.Dispose(true);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }
    }
}